import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.uima.util.FileUtils;

import etxt2db.creator.ComponentCreator;
import etxt2db.parsing.Parser;
import etxt2db.parsing.ParsingOutputData;

/**
 * 
 * Main class used to generate UCompare components from E-txt2db models.
 * 
 * @author Gonçalo Simões
 * @author Rui Lageira
 *
 */
public class Etxt2dbComponentGenerator {

	/**
	 * Main method which captures user arguments specified while running this class.
	 * The arguments have to be the path to the E-txt2db specification file and the 
	 * destination directory where the user wants the component to be saved.
	 * <p>
	 * Parses the specification, generates UCompare components for each training model
	 * created from the specification and saves them to the directory specified.
	 * 
	 * @param args the input arguments of this main class
	 */
	public static void main(String[] args) {
		
		if(args.length != 2) {
			System.err.println("Expecting 2 arguments:\n \t-the path to the specification file\n \t-the destination directory to save the component");
			System.exit(-1);
		}

		// Path to user's specification file.
		String specificationPath = args[0];
		
		// Directory path where components and models will be saved.
		String componentPath = args[1];
		
		Parser p = new Parser(componentPath);
		
		ComponentCreator creator = new ComponentCreator();

		// Converts a file into a String object.
		String query = null;
		try {
			query = FileUtils.file2String(new File(specificationPath));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		System.out.println("Parsing query:\n" + query);
		
		List<ParsingOutputData> info = p.interpretQuery(query);
		
		for(ParsingOutputData outputObject : info) {
			try {
				creator.generateComponentFromModel(outputObject.getAnnotatorName(),componentPath,outputObject.getTypes(),outputObject.getInputs(),p.getMapper().getContent());
			} catch (Exception e) {
				e.printStackTrace();
				System.exit(-1);
			}
		}

	}

}